<?php
class Usuarios_Model_UsuarioMapper {
	protected $_dbTable;
	public function setDbTable($dbTable) {
		if (is_string ( $dbTable )) {
			$dbTable = new $dbTable ();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception ( 'Invalid table data gateway provided' );
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable ( 'Usuarios_Model_DbTable_Usuarios' );
		}
		return $this->_dbTable;
	}
	protected function _fillUsuarios($row,$perfils){
		$registro = new Usuarios_Model_Usuario();
		$data = new Zend_Date ( $row->registro, Zend_Date::ISO_8601 );
		$registro->setId ( $row->id )
			->setUsuario ( $row->usuario )
			->setSenha ( $row->senha )
			->setNome ( $row->nome )
			->setEmail ( $row->email )
			->setTelefone ( $row->telefone )
			->setRegistro ( $data->toString ( Zend_Date::DATE_LONG ) )
			->setId_perfil ( $row->id_perfil )
			->setPerfil ( $perfils );
		
		return $registro;
	}
	public function listUsuarios() {
		$sql = $this->getDbTable ()->select ()->order ( "nome ASC" );
		$resultSet = $this->getDbTable ()->fetchAll ( $sql );
		$entries = array ();
		if ($resultSet->count () > 0) {
			foreach ( $resultSet as $row ) {
				$entries [] = $this->_loadUsuario ( $row );
			}
		}
		return $entries;
	}
	private function _loadUsuario(Zend_Db_Table_Row $row) {
		$usuario = new Usuarios_Model_Usuario ();
		
		$rowsetPerfil = $row->findDependentRowset ( new Usuarios_Model_DbTable_Perfil (), 'Perfil' );
		$perfils = array ();
		if ($rowsetPerfil->count () > 0) {
			foreach ( $rowsetPerfil as $rowPerfil ) {
				$perfil = new Usuarios_Model_Perfil ();
				$perfil->setNome ( $rowPerfil->nome )->setId_parent ( $rowPerfil->id_parent );
				$perfils [] = $perfil;
			}
		}
		$usuario = $this->_fillUsuarios($row,$perfils);
		return $usuario;
	}
	public function findpopule($id, Usuarios_Model_Usuario $usuario) {
		$result = $this->getDbTable ()->find ( $id );
		if (0 == count ( $result )) {
			return;
		}
		$row = $result->current ();
		return $data = array (
				"id" => $row->id,
				"usuario" => $row->usuario,
				"senha" => $row->senha,
				"nome" => $row->nome,
				"email" => $row->email,
				"telefone" => $row->telefone,
				"registro" => $row->registro,
				"id_perfil" => $row->id_perfil 
		);
	}
	public function editUsuario(Usuarios_Model_Usuario $registro, $id = null) {
		// $read = Zend_Auth::getInstance()->getStorage()->read();
		$data = array (
				'nome' => $registro->getNome (),
				'email' => $registro->getEmail (),
				'telefone' => $registro->getTelefone (),
				'id_perfil' => $registro->getId_perfil () 
		);
		$this->getDbTable ()->update ( $data, array (
				'id = ?' => $id 
		) );
	}
	public function saveUser(Usuarios_Model_Usuario $registro) {
		$data = array (
				'usuario' => $registro->getUsuario (),
				'senha' => sha1 ( $registro->getSenha () ),
				'nome' => $registro->getNome (),
				'email' => $registro->getEmail (),
				'telefone' => $registro->getTelefone (),
				'id_perfil' => $registro->getId_perfil () 
		);
		$this->getDbTable ()->insert ( $data );
	}
	public function deletar($id = NULL) {
		try {
			$result = $this->getDbTable ()->find ( $id );
			$this->getDbTable ()->delete ( array (
					'id = ?' => $id 
			) );
		} catch ( Zend_Exception $e ) {
			echo $e;
		}
	}
	public function fetchAll() {
		$sql = $this->getDbTable ()->select ()->order ( "nome ASC" );
		$resultSet = $this->getDbTable ()->fetchAll ( $sql );
		$entries = array ();
		foreach ( $resultSet as $row ) {
			$entries [] = $this->_fillUsuarios($row);
		}
		return $entries;
	}
}

